/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.model.core;

import org.eclipse.emf.ecore.EClass;
import org.w3c.dom.Element;

import edu.pku.sei.gefcommon.descriptor.annotation.ListProperty;
import edu.pku.sei.gefcommon.descriptor.annotation.MethodProperty;
import edu.pku.sei.modeler.model.common.MetaTypedList;
import edu.pku.sei.modeler.model.xml.XMLUtility;
/**
 * @author zl
 * @author xy
 *
 */
public class MetaOperation extends MetaTypedElement {
	MetaClass containingClass;
	MetaTypedList exceptions;
	MetaTypedList parameters;
	
	@MethodProperty(
			id = "OPERATION_CONTAININGCLASS",
			catalog = "Model",
			name = CorePackage.OPERATION_CONTAININGCLASS,
			setter = "setContainingClass"
			)
	public MetaClass getContainingClass(){
		return containingClass;
	}
	
	public void setContainingClass(MetaClass newContainingClass) {
		MetaClass oldContainingClass = containingClass;
		containingClass = newContainingClass;
		firePropertyChange(CorePackage.OPERATION_CONTAININGCLASS, oldContainingClass, newContainingClass);
	}
	
	/*@ListProperty(
			ID = "OPERATION_EXCEPTIONS",
			Catalog = "Model",
			Name = CorePackage.OPERATION_EXCEPTIONS,
			ElementType = CorePackage.)*/
	public MetaTypedList getExceptions() {
		if(exceptions == null) {
			exceptions = new MetaTypedList(MetaClassifier.class, this, CorePackage.OPERATION_EXCEPTIONS);
		}
		return exceptions;
	}
	
//	@ListProperty(
//			ID = "OPERATION_PARAMETERS",
//			Catalog = "Model",
//			Name = CorePackage.__METAPARAMETER__
//			)
	public MetaTypedList getParameters() {
		if(parameters == null) {
			parameters = new MetaTypedList(MetaParameter.class, this, CorePackage.OPERATION_PARAMETERS);
		}
		return parameters;
	}
	
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeNodeNotContain(root, CorePackage.OPERATION_CONTAININGCLASS, containingClass);
		XMLUtility.xmlInstance.serializeNodes(root, CorePackage.OPERATION_EXCEPTIONS, exceptions);
		XMLUtility.xmlInstance.serializeNodes(root, CorePackage.OPERATION_PARAMETERS, parameters);
	}
	
	public void deSerialize(Element root) {
		super.deSerialize(root);
		containingClass = (MetaClass)XMLUtility.xmlInstance.deSerializeNodeNotContain(root, CorePackage.OPERATION_CONTAININGCLASS,this);
		this.getExceptions().addAll(XMLUtility.xmlInstance.deSerializeNodes(root, CorePackage.OPERATION_EXCEPTIONS));
		this.getParameters().addAll(XMLUtility.xmlInstance.deSerializeNodes(root, CorePackage.OPERATION_PARAMETERS));
	}
	
	@Override
	protected EClass eStaticClass() {
		return EMetaModelPackage.Literals.EMETA_OPERATION;
	}
	
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case EMetaModelPackage.EMETA_OPERATION__CONTAINING_CLASS:
				if (resolve) return getContainingClass();
				return getContainingClass();
			case EMetaModelPackage.EMETA_OPERATION__EXCEPTIONS:
				return getExceptions();
			case EMetaModelPackage.EMETA_OPERATION__PARAMETERS:
				return getParameters();
		}
		return super.eGet(featureID, resolve, coreType);
	}
	
}
